﻿using TestBat.IModels.Services;
using TestBat.IViewModels.Factories;
using TestBat.IViewModels.Windows;
using TestBat.IViewModels.Workspaces;
using TestBat.IViewModels.Workspaces.Gradings;
using TestBat.IViewModels.Workspaces.Results;
using TestBat.IViewModels.Workspaces.Settings;
using TestBat.ServiceLocator;
using TestBat.ViewModels.Attributes;
using TestBat.ViewModels.Base;

namespace TestBat.ViewModels.Windows
{
    /// <summary>
    /// TODO: dependency injection
    /// </summary>
    public class MainWindowViewModel : DialogViewModelContainer, IMainWindowViewModel
    {
        public MainWindowViewModel(ITasksViewModelFactory tasksViewModelFactory, ISchedulesViewModelFactory schedulesViewModelFactory, ITraineesViewModelFactory traineesViewModelFactory)
        {
            this.tasksViewModelFactory = tasksViewModelFactory;
            this.schedulesViewModelFactory = schedulesViewModelFactory;
            this.traineesViewModelFactory = traineesViewModelFactory;
        }

        [BusinessErrorDialog]
        [DatabaseErrorDialog]
        public string ActiveTab
        {
            set
            {
                switch (value)
                {
                    case "Schedules":
                        ActiveWorkspace = schedulesViewModelFactory.Create();
                        break;
                    case "Tasks":
                        ActiveWorkspace = tasksViewModelFactory.Create();
                        break;
                    case "Trainees":
                        ActiveWorkspace = traineesViewModelFactory.Create();
                        break;
                    case "Gradings":
                        ActiveWorkspace = DefaultServiceLocator.Get<IGradingsViewModel>();
                        break;
                    case "Results":
                        ActiveWorkspace = DefaultServiceLocator.Get<IResultsViewModel>();
                        break;
                    case "Settings":
                        ActiveWorkspace = DefaultServiceLocator.Get<ISettingsViewModel>();
                        break;
                }
            }
        }

        public IWorkspaceViewModel ActiveWorkspace
        {
            get
            {
                return activeWorkspace;
            }
            private set
            {
                if (activeWorkspace != value)
                    ChangeWorkspace(value);
            }
        }

        //[CheckUnsavedChanges]
        private void ChangeWorkspace(IWorkspaceViewModel to)
        {
            if (!(to is ISettingsViewModel))
                DefaultServiceLocator.Get<IConversationService>().Abort(); // cancel the conversation


            activeWorkspace = to;
            NotifyPropertyChanged(() => this.ActiveWorkspace);
        }

        private readonly ITasksViewModelFactory tasksViewModelFactory;
        private readonly ISchedulesViewModelFactory schedulesViewModelFactory;
        private readonly ITraineesViewModelFactory traineesViewModelFactory;

        private IWorkspaceViewModel activeWorkspace;
    }
}
